import axios from 'axios'

// 超时时间
axios.defaults.timeout = 30000;

// 跨域请求，允许保存cookie
axios.defaults.withCredentials = true
const token = window.localStorage.getItem('token');
if (token) {
  axios.defaults.headers.common['Authorization'] = `Bearer `+  token;
}
// HTTPrequest拦截
axios.interceptors.request.use(config => {
  return Promise.resolve(config)
}, error => {
  return Promise.reject(error)
})
// HTTPresponse拦截
axios.interceptors.response.use(data => {
  return data
}, error => {
  Message({
    message: errorCode[code] || errorCode['default'],
    type: 'error'
  })
  return Promise.reject(new Error(error))
})

export default axios
